package vn.com.vega.music.view.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.chacha.v2.VideoPlayerScreen;
import vn.com.vega.chacha.v2.Asynctask.DoSomthingListener;
import vn.com.vega.chacha.v2.Asynctask.FavoriteVideo;
import vn.com.vega.music.downloadmanager.DownloadUtil;
import vn.com.vega.music.objects.Video;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter.AutoLoadingInterface;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class VideoListAdapter extends BaseAdapter implements Const,
		AutoLoadingInterface {
	private Activity mContext;
	// private ImageLoader mImageLoader;
	private Appinstant mApp;
	private LayoutInflater mInflater;
	private List<Object> mListVideo = new ArrayList<Object>();
	private LinearLayout mCurrentMenuShow = null;

	private boolean isLocalMusic = false;

	public VideoListAdapter(Activity context, List<Object> objects,
			boolean _isLocalMusic) {
		mContext = context;
		isLocalMusic = _isLocalMusic;
		// mImageLoader = ImageLoader.getInstance(context);
		mApp = (Appinstant) context.getApplication();
		mInflater = LayoutInflater.from(context);
		if (objects != null) {
			mListVideo = objects;
		}
	}

	public void setIsLocalMusic(boolean value) {
		isLocalMusic = value;
	}

	@Override
	public int getCount() {
		return mListVideo.size();
	}

	@Override
	public Object getItem(int position) {
		return mListVideo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		Object obj = mListVideo.get(position);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.video_item, null);
			holder = new ViewHolder();
			holder.thumb = (ImageView) convertView.findViewById(R.id.imv_thumb);
			holder.title = (TextView) convertView
					.findViewById(R.id.tv_song_title);
			holder.artist = (TextView) convertView
					.findViewById(R.id.tv_artist_name);
			holder.listtenCount = (TextView) convertView
					.findViewById(R.id.tv_count_song_listen);
			holder.videoTime = (TextView) convertView
					.findViewById(R.id.tv_video_time);
			holder.layoutShowHideMenu = (LinearLayout) convertView
					.findViewById(R.id.layout_show_hide_menu_song_item);
			holder.layoutMenu = (LinearLayout) convertView
					.findViewById(R.id.layout_menus_song_item);
			holder.videoItem = convertView.findViewById(R.id.layout_video_item);
			holder.dowloadVideo = (ImageButton) convertView
					.findViewById(R.id.bt_dowload_video);
			holder.likeVideo = (ImageButton) convertView
					.findViewById(R.id.bt_like_video);
			holder.shareVideo = (ImageButton) convertView
					.findViewById(R.id.bt_share_video);
			holder.dowloadVideo.setOnClickListener(onDownloadClickListener);
			holder.likeVideo.setOnClickListener(onLikeClickListener);
			holder.shareVideo.setOnClickListener(onShareClickListener);

			holder.layoutShowHideMenu
					.setOnClickListener(showHideMenuClickListener);
			holder.layoutShowHideMenu.setTag(holder);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		

		holder.thumb.setVisibility(View.GONE);
		holder.layoutMenu.setVisibility(View.GONE);
		Video video = (Video) obj;
		holder.dowloadVideo.setTag(video);
//		holder.dowloadVideo.setVisibility(View.GONE);
		holder.likeVideo.setTag(video);
		if (video.isFavorite) {
			holder.likeVideo.setSelected(true);
		} else {
			holder.likeVideo.setSelected(false);
		}
		if (video.isDownloaded()) {
			holder.listtenCount.setVisibility(View.GONE);
			holder.dowloadVideo.setVisibility(View.GONE);
		} else {
			holder.dowloadVideo.setVisibility(View.VISIBLE);
			holder.listtenCount.setVisibility(View.VISIBLE);
		}
		
		holder.shareVideo.setTag(video);
		holder.title.setText(video.title);
		holder.artist.setText(video.artistName);
		holder.listtenCount.setText(video.viewCount + "");
		holder.videoTime.setText(Util.intToStringTimeFormat(video.duration));
		holder.thumb.setVisibility(View.VISIBLE);
		mApp.mImageLoader.download(video.thumbnail_small, holder.thumb);

		final int index = position;
		holder.videoItem.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				openVideo(mListVideo, index);
			}
		});

		return convertView;
	}
	
	

	private void openVideo(List<Object> listVideoObj, int index) {
		List<Video> listVideo = new ArrayList<Video>();
		for (Object object : listVideoObj) {
			Video video = (Video) object;
			listVideo.add(video);
		}
		String videlListName = VideoListAdapter.class.getName();
		mApp.tranportListVideo.put(videlListName, listVideo);
		Intent intent = new Intent(mContext, VideoPlayerScreen.class);
		intent.putExtra(VideoPlayerScreen.VIDEO_LIST_NAME, videlListName);
		intent.putExtra(VideoPlayerScreen.INDEX_VIDEO, index);
		mContext.startActivity(intent);
	}

	View.OnClickListener showHideMenuClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			ViewHolder holder = (ViewHolder) v.getTag();
			if (mCurrentMenuShow == holder.layoutMenu) {
				if (holder.layoutMenu.isShown()) {
					holder.layoutMenu.setVisibility(View.GONE);
				} else {
					holder.layoutMenu.setVisibility(View.VISIBLE);
				}
			} else {
				if (mCurrentMenuShow != null) {
					mCurrentMenuShow.setVisibility(View.GONE);
				}
				holder.layoutMenu.setVisibility(View.VISIBLE);
				mCurrentMenuShow = holder.layoutMenu;
			}
		}
	};

	View.OnClickListener onShareClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mCurrentMenuShow.setVisibility(View.GONE);
			Video video = (Video) v.getTag();
			if (video == null) {
				return;
			}
			Util.share(video.shareMessage, mContext);
		}
	};
	
	private void showToast(String msg) {
		if (Util.checkStringIsemptyOrNull(msg)) {
			return;
		}
		if (mContext instanceof ActivityBase) {
			ActivityBase act = (ActivityBase) mContext;
			act.showToast(msg);
		}
	}

	DoSomthingListener doLikeVideoListener = new DoSomthingListener() {

		@Override
		public void startDoSomthing(List<Object> extractObject) {
			// TODO Auto-generated method stub
			String message = (String) extractObject.get(0);
			showToast(message);
		}

		@Override
		public void doSomethingSuccess(List<Object> extractObject) {
			// TODO Auto-generated method stub
			String message = (String) extractObject.get(1);
			if (!Util.checkStringIsemptyOrNull(message)) {
				showToast(message);
			}
			notifyDataSetChanged();

		}

		@Override
		public void doSomethingFail(List<Object> extractObject) {
			// TODO Auto-generated method stub
			String errorMessage = (String) extractObject.get(1);
			if (!Util.checkStringIsemptyOrNull(errorMessage)) {
				showToast(errorMessage);
			}
		}
	};

	View.OnClickListener onLikeClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mCurrentMenuShow.setVisibility(View.GONE);
			Video video = (Video) v.getTag();
			FavoriteVideo favoriteVideo;
			if (video.isFavorite) {
				favoriteVideo = new FavoriteVideo((ActivityBase) mContext, video, doLikeVideoListener,
						false);
			} else {
				favoriteVideo = new FavoriteVideo((ActivityBase) mContext, video, doLikeVideoListener,
						true);
			}
			favoriteVideo.execute();
		}
	};

	View.OnClickListener onDownloadClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mCurrentMenuShow.setVisibility(View.GONE);
			Video video = (Video) v.getTag();
			if (DownloadUtil.checkVideoInListDownload(video)) {
				((ActivityBase)mContext).showToast("Video đã được thêm vào danh sách tải về");
				return;
			}
			DownloadUtil.downloadVideo(video, mContext);
		}
	};

	static class ViewHolder {
		ImageView thumb;
		TextView title;
		TextView artist;
		TextView listtenCount;
		TextView videoTime;
		LinearLayout layoutShowHideMenu;
		LinearLayout layoutMenu;
		View videoItem;

		ImageButton dowloadVideo;
		ImageButton likeVideo;
		ImageButton shareVideo;
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged) {
		notifyListObjectChanged(objectsChanged, TYPE_LIST_ADDED);
	}

	@Override
	public void notifyListObjectChanged(List<Object> objectsChanged, int type) {
		if (objectsChanged != null) {
			if (type == TYPE_LIST_REPLACED) {
				mListVideo.clear();
			}
			mListVideo.addAll(objectsChanged);
			notifyDataSetChanged();
		}
	}

	public List<Object> getListObject() {
		// TODO Auto-generated method stub
		return mListVideo;
	}
}
